package com.guanyong.admin.controllers.api;

import com.guanyong.model.RoleModel;
import com.guanyong.services.RoleService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by mxk on 2017/6/7.
 */
@RestController
@RequestMapping("/api/role/")
public class RoleApi {

    @Autowired
    RoleService roleService;


    @RequestMapping(value = "rolelist", method = RequestMethod.GET)
    ResultObject<List<RoleModel>> roleList(HttpServletRequest request) {
        ResultObject<List<RoleModel>> result = new ResultObject<List<RoleModel>>();
        List<RoleModel> list=roleService.GetRoleAll();
        result.setResult(list);
        return result;
    }//updatestate


    @RequestMapping(value = "addrole", method = RequestMethod.POST)
    ResultObject<Integer> addRole(HttpServletRequest request,
                                      @RequestParam String  role_name,
                                      @RequestParam String role_desc,
                                      @RequestParam String role_func){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        RoleModel roleModel=new RoleModel();
        roleModel.setfName(role_name);
        roleModel.setfDesc(role_desc);
        roleModel.setfFuncs(role_func);

        Integer res=roleService.AddRole(roleModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }


    @RequestMapping(value = "updateRole", method = RequestMethod.POST)
    ResultObject<Integer> updateRole(HttpServletRequest request,
                                      @RequestParam Integer role_id,
                                      @RequestParam String  role_name,
                                      @RequestParam String role_desc,
                                      @RequestParam String role_func){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        RoleModel roleModel=new RoleModel();
        roleModel.setfId(role_id);
        roleModel.setfName(role_name);
        roleModel.setfDesc(role_desc);
        roleModel.setfFuncs(role_func);

        Integer res=roleService.UpdateRole(roleModel);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }
}
